package com.ovhoo.vdm.vdm;

import java.util.ArrayList;

import com.ovhoo.vdm.vdm.VDMQuotesList.requestType;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class VDMQuoteCommentsList extends ListActivity {
	
	public enum requestType  {LOAD_RANDOM_QUOTES_COMMENT, LOAD_QUOTE_COMMENTS_BY_ID};
	private VDMQuoteCommentsListAdapter listAdapter;
	private ProgressDialog waitingDialog;
	private VDMControler vdmcontroler;
	
	private VDMQuote quote = null;
	
	Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			ArrayList<VDMQuote> _quotes = (ArrayList<VDMQuote>) msg.obj;
			if (_quotes != null){
				if (_quotes.size() > 0){
					quote = _quotes.get(0);
				}
			}
			
			listAdapter.setQuotes(_quotes);
			
			waitingDialog.dismiss();
			
			listAdapter.notifyDataSetChanged();
		}
	};

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vdmlist);
        
        
        this.listAdapter = new VDMQuoteCommentsListAdapter(this);
        setListAdapter(this.listAdapter);
        
        
        //Fenetre d'attente
        this.waitingDialog = ProgressDialog.show(this, this.getResources().getString(R.string.vdm_progresdialog_title), this.getResources().getString(R.string.vdm_progresdialog_text));
        
        
        //Thread : controler
        this.vdmcontroler = VDMControler.getInstance();
    }
    
    public void onStart(){
    	super.onStart();
    	
    	
    	Bundle extras  = getIntent().getExtras();
        if(extras != null){
        	requestType _paramIntent  = this.int2requestType( extras.getInt("com.ovhoo.vdm.vdm.VDM_QUOTE_COMMENTS_LIST_TYPE")); 
        	int _quoteID = extras.getInt("com.ovhoo.vdm.vdm.VDM_QUOTE_COMMENTS_LIST_ID");
        		
        	if (_paramIntent == requestType.LOAD_QUOTE_COMMENTS_BY_ID){
        		this.setTitle(this.getResources().getString(R.string.quotes_comments_by_id));
        		this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_QUOTE_COMMENTS_BY_ID, _quoteID, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}
        	else{
        		this.setTitle(this.getResources().getString(R.string.random_quotes));
        		this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_RANDOM_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}

        }
        else{
        	this.setTitle(this.getResources().getString(R.string.random_quotes));
        	this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_RANDOM_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        }
    	
    }
    
    
    public requestType int2requestType(int param){
    	return requestType.class.getEnumConstants()[param];
    }
}
